package middleware

import (
	"Walker/app/utils/jwt"
	"Walker/app/utils/response"
	"fmt"
	"github.com/gin-gonic/gin"
	"strings"
)

func TokenUser(ctx *gin.Context) {
	//获取token
	var authorization string
	var token []string
	if authorization = ctx.GetHeader("authorization"); authorization == "" {
		response.AuthError(ctx, "无法解析token")
		return
	}
	if token = strings.Split(authorization, " "); len(token) != 2 {
		response.AuthError(ctx, "无法解析token")
		return
	}
	if err := jwt.VerifyToken(token[1]); err != nil {
		response.AuthError(ctx, err.Error())
		return
	}
	//先判断token是否刷新时间也过期了
	if claims, err := jwt.ParseToken(token[1]); err != nil {
		response.AuthError(ctx, err.Error())
		return
	} else {
		fmt.Println(claims.ExpiresAt)
		ctx.Set("UID", claims.UserId)
	}
	ctx.Next()
}
